﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TierApplication;
using System.Data;
using System.Text;

public partial class ForeProgram_product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["Bandwidth"] != null)
            {
                ViewState["type"] = "Bandwidth";
                ViewState["cond"] = Request.QueryString["Bandwidth"];
            }
            if (Request.QueryString["LifeTime"] != null)
            {
                ViewState["type"] = "LifeTime";
                ViewState["cond"] = Request.QueryString["LifeTime"];
            }
            if (Request.QueryString["CategoryId"] != null)
            {
                ViewState["type"] = "CategoryId";
                ViewState["cond"] = Request.QueryString["CategoryId"];
            }
            if (Request.QueryString["keyWord"] != null)
            {
                ViewState.Clear();
                ViewState["keyWord"] = Request.QueryString["keyWord"];
            }
            BindTable();
        }
        if (txtList.Text == "" || txtList.Text.Equals("grid"))
        {
            showGrid.Src = "images/display_mode_1.gif";
            showBar.Src = "images/display_mode_2_2.gif";
        }
        else
        {
            showGrid.Src = "images/display_mode_1_2.gif";
            showBar.Src = "images/display_mode_2.gif";
        }
        
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindTable();
    }

    /// <summary>
    /// 绑定 GridView
    /// </summary>
    private void BindTable()
    {
        try
        {
            T_PRODUCT prod = new T_PRODUCT();
            StringBuilder sb = new StringBuilder();
            sb.Append(" 1=1 ");
            //sb.Append(" impstatus='1'");
            if (ViewState["type"] != null && ViewState["cond"] != null)
            {
                if(!ViewState["cond"].Equals(""))
                {
                    sb.Append(" And " + ViewState["type"].ToString());
                    sb.Append(" ='" + ViewState["cond"].ToString() + "'");
                }
            }
            if (ViewState["keyWord"] != null)
            {
                sb.Append(" And productname like '" + ViewState["keyWord"].ToString()+"%'");
            }
            sb.Append(" And schoolid ='" + Security.GetSchoolID() + "'");
            DataSet ds = prod.GetList(sb.ToString());

            //if (dtResult != null && dtResult.Rows.Count > 0)
            //{
            int iCount = ds.Tables[0].Rows.Count;

            PagedDataSource pds = new PagedDataSource();
            AspNetPager1.RecordCount = iCount;
            pds.DataSource = ds.Tables[0].AsDataView();
            pds.AllowPaging = true;
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            pds.PageSize = AspNetPager1.PageSize;

            if (iCount > AspNetPager1.PageSize)
            {
                AspNetPager1.Visible = true;
            }
            else
            {
                AspNetPager1.Visible = false;
            }
            dlProduct.Visible = false;
            dlBar.Visible = false;
            if (txtList.Text == "" || txtList.Text.Equals("grid"))
            {
                dlProduct.Visible = true;
                dlProduct.DataSource = pds;
                dlProduct.DataBind();
            }
            else
            {
                dlBar.Visible = true;
                dlBar.DataSource = pds;
                dlBar.DataBind();
            }

            //}
            //else
            //{
            //    //SetInitStatus();
            //}
        }
        catch (Exception ex)
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('绑定数据时出错！');</script>");
            return;
        }
        this.AspNetPager1.CustomInfoHTML = string.Format("共 <font color='red'>{0}</font> 条记录 <font color='red'>{1}/{2}</font>", new object[] { AspNetPager1.RecordCount, AspNetPager1.CurrentPageIndex, AspNetPager1.PageCount });
    }

    protected void dlProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemIndex == -1)
            return;
        Image img = (Image)e.Item.FindControl("imgProduct");
        Label name = (Label)e.Item.FindControl("lblProductname");
        Label marke = (Label)e.Item.FindControl("lblMarketprice");
        Label Discountprice = (Label)e.Item.FindControl("lblDiscountprice");
        HyperLink open = (HyperLink)e.Item.FindControl("hlOpen");
        HyperLink open1 = (HyperLink)e.Item.FindControl("hlOpen1");
        img.ImageUrl = "~/ProductImages/" + DataBinder.Eval(e.Item.DataItem, "productimage").ToString();
        name.Text = DataBinder.Eval(e.Item.DataItem, "Productname").ToString();
        marke.Text ="市场价：￥"+ DataBinder.Eval(e.Item.DataItem, "Marketprice").ToString();
        Discountprice.Text = "优惠价：<b>￥" + DataBinder.Eval(e.Item.DataItem, "unitprice").ToString() + "</br>";
        open1.ToolTip = name.Text;
        open.NavigateUrl = "ProductView.aspx?Productid=" + ((DataList)sender).DataKeys[e.Item.ItemIndex].ToString();
        open1.NavigateUrl = "ProductView.aspx?Productid=" + ((DataList)sender).DataKeys[e.Item.ItemIndex].ToString();
        Button buy = (Button)e.Item.FindControl("btnBuy");
        if(buy!=null)
            buy.CommandArgument = e.Item.ItemIndex.ToString();
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindTable();
    }
    protected void dlBar_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Buy")
        {
            Context.Response.Redirect("ProductView.aspx?Productid=" + dlBar.DataKeys[Convert.ToInt32(e.CommandArgument)].ToString());
        }
    }
}